<?php

namespace wicked\core;

abstract class Session
{

    use \wicked\tools\StaticAccessor;

    protected static $rank = 0;

    protected static $data = [];

    protected static $flash = [];

    protected static $history = [];

    protected static $user;

    public static function logout()
    {
        self::$rank = 0;
        self::$user = null;
    }

    public static function open()
    {
        Event::fire('session.start');

        $session = isset($_SESSION['WICKED_SESSION']) ? (array)unserialize($_SESSION['WICKED_SESSION']) : [];
        static::$rank = isset($session['rank']) ? $session['rank'] : 0;
        static::$data += isset($session['rank']) ? (array)$session['data'] : [];
        static::$flash += isset($session['flash']) ? (array)$session['data'] : [];
        static::$history += isset($session['history']) ? (array)$session['data'] : [];
        static::$user = isset($session['user']) ? $session['user'] : null;
    }

    public static function close()
    {
        $session = [
            'rank' => static::$rank,
            'data' => static::$data,
            'flash' => static::$flash,
            'user' => static::$user
        ];
        $_SESSION['WICKED_SESSION'] = serialize($session);

        Event::fire('session.end');
    }

}
